﻿using System;
using System.Collections.Generic;
using Core.Controllers;
using Core.Controllers.Helpers;
using Core.Model;
using Core.Model.Chunks;
using Core.Model.Types;

namespace Core.View.Renders
{
    public class SkyDomeRenderer
    {
        private int _vertexCount;
        private int _indexCount;

        protected int IndexCount
        {
            get { return _indexCount; }
        }

        protected int VertexCount
        {
            get { return _vertexCount; }
        }

        public virtual void LoadContent()
        {

        }

        public virtual void Draw(Camera camera)
        {

        }

        public Vector3F GetDirection()
        {
            return new Vector3F((float) (Math.Sin(fTheta)*Math.Cos(fPhi)),
                                (float) Math.Cos(fTheta),
                                (float) (Math.Sin(fTheta)*Math.Sin(fPhi)));
        }

        private float _minutos = 5*60+40;

        protected float fTheta;
        protected float fPhi = 40f;

        public int TimeOfDay
        {
            get { return (int)_minutos/60; }
        }

        public void Update()
        {
            _minutos += 0.01f;

            if (_minutos > 24 * 60)
            {
                _minutos = 1;
            }

            fTheta = _minutos*(float) (Math.PI)/12.0f/60.0f;
        }

        protected void Calculate(out List<SkyDomeVertex> vertices, out List<int> vIndexes)
        {
            int m_sGridLevelsCount = 40;

            int m_fGridRadius = ChunkManager.SKY_DOME_Diametr;

            vertices = new List<SkyDomeVertex>();

            Vector3F position;

            for (int v = 0; v < m_sGridLevelsCount; v++)
            {
                for (int u = 0; u < m_sGridLevelsCount; u++)
                {
                    float al = -2*MathHelper.Pi*((float) u/(m_sGridLevelsCount - 1.0f));
                    float th = 0.6f * MathHelper.Pi * ((float)v / (m_sGridLevelsCount - 1.0f));

                    position = new Vector3F((float) (Math.Sin(th)*Math.Sin(al))*m_fGridRadius,
                                            (float)Math.Cos(th) * m_fGridRadius,
                                            (float) (Math.Sin(th)*Math.Cos(al))*m_fGridRadius);

                    vertices.Add(new SkyDomeVertex(position));
                }
            }

            vIndexes = new List<int>();

            for (int v = 0; v < m_sGridLevelsCount; v++)
            {

                for (int u = 0; u < m_sGridLevelsCount; u++)
                {
                    // face 1 |/
                    vIndexes.Add(v*m_sGridLevelsCount + u);
                    vIndexes.Add(v * m_sGridLevelsCount + u + 1);
                    vIndexes.Add((v + 1) * m_sGridLevelsCount + u);

                    // face 2 /|
                    vIndexes.Add((v + 1) * m_sGridLevelsCount + u);
                    vIndexes.Add(v * m_sGridLevelsCount + u + 1);
                    vIndexes.Add((v + 1) * m_sGridLevelsCount + u + 1);
                }
            }

            _vertexCount = vertices.Count;
            _indexCount = vIndexes.Count;
        }
    }
}